var express = require('express');
var path = require('path');
var bodyParser = require('body-parser');
var jwt = require('express-jwt')

//路由 
var users= require('./routes/user.js');
var api = require('./routes/api.js')
var app = express();

var port = 3030;
//调用工具 
app.use(bodyParser.json());
// app.use(jwt({
//   secret: 'hello',
//   credentialsRequired: false,
//   getToken: function fromHeaderOrQuerystring (req) {
//     if (req.headers.authorization && req.headers.authorization.split(' ')[0] === 'Bearer') {
//         return req.headers.authorization.split(' ')[1];
//     } else if (req.query && req.query.token) {
//       return req.query.token;
//     }
//     return null;
//   }
// }));
//  app.use(express.static(path.join(__dirname, 'client')));
//allow custom header and CORS
app.all('*',function (req, res, next) {
  res.header('Access-Control-Allow-Origin', '*');
  res.header('Access-Control-Allow-Headers', 'Content-Type, Content-Length, Authorization, Accept, X-Requested-With , yourHeaderFeild');
  res.header('Access-Control-Allow-Methods', 'PUT, POST, GET, DELETE, OPTIONS');
  if (req.method == 'OPTIONS') {
    res.send(200); /让options请求快速返回/
  }
  else {
    next();
  }
})
// app.get('/',function(req,res){
//     res.sendfile('./views/index.html')
// })
app.use('/users', users);
app.use('/api',api);
app.listen(port, function(){
    console.log(`server is runing on ${port}`)
});